package com.sinosoft.hydra.um.services.component.dataimport;

import com.sinosoft.hydra.logging.LogModules;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2015/1/23.
 */
public class ExcelReader implements Serializable {

    private static String XLS_TYPE_STRING = "xls";
    private static String XLSX_TYPE_STRING = "xlsx";

    private String getExtName(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    public List<Row> readData(InputStream inputStream, String fileName) {
        Sheet sheet;
        String extName = getExtName(fileName);
        List<Row> rows = new ArrayList<Row>();
        try {
            if (XLS_TYPE_STRING.equals(extName)) {
                POIFSFileSystem fs = new POIFSFileSystem(inputStream);
                sheet = new HSSFWorkbook(fs).getSheetAt(0);
            } else if (XLSX_TYPE_STRING.equals(extName)) {
                sheet = new XSSFWorkbook(inputStream).getSheetAt(0);
            } else {
                LoggerFactory.getLogger(LogModules.STD_LOGGER).error("文件的扩展名不正确，不能正确解析excel");
                return Collections.emptyList();
            }
            if (sheet != null) {
                int i = 1, last = sheet.getLastRowNum();
                for (; i <= last; i++) {
                    rows.add(sheet.getRow(i));
                }
            }
            return rows;
        } catch (IOException e) {
            LoggerFactory.getLogger(LogModules.STD_LOGGER).error("excel文件解析失败，没有获取到数据{}", e.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LoggerFactory.getLogger(LogModules.STD_LOGGER).error("{}", e.getMessage());
                }
            }
        }
        return Collections.emptyList();
    }


    public static Object getCellData(Cell cell) {
        if (cell != null) {
            // 判断当前Cell的Type
            switch (cell.getCellType()) {
                // 如果当前Cell的Type为NUMERIC
                case HSSFCell.CELL_TYPE_NUMERIC:
                case HSSFCell.CELL_TYPE_FORMULA: {
                    return HSSFDateUtil.isCellDateFormatted(cell)
                            ? cell.getDateCellValue()
                            : cell.getNumericCellValue();
                }
                // 如果当前Cell的Type为STRIN
                case HSSFCell.CELL_TYPE_STRING:
                    // 取得当前的Cell字符串
                    return cell.getStringCellValue();
                // 默认的Cell值
                case Cell.CELL_TYPE_BLANK:
                    return StringUtils.EMPTY;
                case Cell.CELL_TYPE_BOOLEAN:
                    return cell.getBooleanCellValue();
                default:
                    return null;
            }
        }
        return null;
    }
}
